package com.wensi.lib_network

import com.wensi.lib_network.http.HttpKey
import com.wensi.lib_network.http.HttpUrl
import com.wensi.lib_network.impl.HttpImplService
import com.wensi.lib_network.intercepter.HttpInterceptor
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 根据城市查询天气
 * 专门对外的网络管理工具类
 */
object HttpManager {
    private fun getClient(): OkHttpClient {
        return OkHttpClient.Builder().addInterceptor(HttpInterceptor()).build()
    }
    // 天气的对象
    private val retrofitWeather by lazy {
        Retrofit.Builder()
            .baseUrl(HttpUrl.WEATHER_BASE_URL)
            .client(getClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    // 天气接口对象
    private val apiWeather by lazy {
       retrofitWeather.create(HttpImplService::class.java)
    }

    // 查询天气
    fun queryWeather(city: String): Call<ResponseBody> {
       return apiWeather.getWeather(city, HttpKey.WEATHER_KEY)
    }
}